package com.s53.crm.clue.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.s53.crm.clue.service.IClueService;
import com.s53.crm.entity.Clue;
import com.s53.crm.entity.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 线索 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-04-19
 */
@RestController
@RequestMapping("/clues")
@RequiredArgsConstructor
@RefreshScope
public class ClueController {
    @Value("${server.port}")
    private int port;
    @Value("${email}")
    private String email;
    @Value("${subject}")
    private String subject;

    private final IClueService clueService;


    @PostMapping("/list")
    @SentinelResource(value = "clueList", blockHandler = "handlerBlock")
    public R list(@RequestBody Clue clue,
                  @RequestParam("current")int current,
                  @RequestParam("size") int size) {
        System.out.println(port + "," + email + "," + subject);
        LambdaQueryWrapper<Clue> queryWrapper
                = Wrappers.lambdaQuery(Clue.class).like(StringUtils.hasText(clue.getName()), Clue::getName, clue.getName());
        return R.ok(clueService.page(Page.of(current, size), queryWrapper));
    }

    public R handlerBlock(Clue clue, int current, int size, BlockException ex) {
        return R.err(503, "限流了");
    }
}
